import { _decorator, Component, EventTouch, Game, game, Node, sp, Sprite, SpriteFrame, tween, Vec3 } from 'cc';

import { UserInfo } from '../common/UserInfo';
import { EventManager } from '../event/EventManager';
import { Space } from './Space';
import { Building } from '../entity/Buildings';
import { Events } from '../entity/Events';
import { VillageCamera } from './VillageCamera';
import { BManager } from '../entity/BManager';
import ResManager from '../common/ResManager';
import { AudioManager, Mp3Enum } from '../common/AudioManager';

const { ccclass, property } = _decorator;



@ccclass('VillageController')
export class VillageController extends Component {

    @property(Sprite)
    cityBg: Sprite
    @property(Sprite)
    wallImage: Sprite

    public static instance: VillageController;
    /**地图上所有的建筑坑位 */
    public spaceList: Node;
    /**当前点击选中的坑位 */
    public curClickSpace: Node;
    /**当前预选中的建筑物 */
    public curBuilding: Building

    protected onLoad(): void {
        VillageController.instance = this;
    }

    start() {


        this.spaceList = this.node.getChildByPath('Stage/spaceList')

        EventManager.instance.on(EventManager.EventType.GetVillageDetail, this.renderBuildings, this)

        EventManager.instance.on(EventManager.EventType.SpaceClick, this.onSpaceClick, this)

        EventManager.instance.on(EventManager.EventType.BGCLICK, this.onBgClick, this)

        game.on(Game.EVENT_SHOW, this.onShow, this)
        
        AudioManager.instance.playClip(Mp3Enum.innercity_0)

    }

    protected onDisable(): void {
        EventManager.instance.off(EventManager.EventType.GetVillageDetail, this.renderBuildings, this)

        EventManager.instance.off(EventManager.EventType.SpaceClick, this.onSpaceClick, this)

        EventManager.instance.off(EventManager.EventType.BGCLICK, this.onBgClick, this)

        game.off(Game.EVENT_SHOW, this.onShow, this)
    }

    onShow() {
        console.log('on show....')
        UserInfo.instance.refreshVillageDetail()
    }
    /**渲染地图上的建筑物 */
    renderBuildings() {
        
        ResManager.instance.loadAssetByUrl<SpriteFrame>(`cityImg/city_bg_${UserInfo.instance.race}/spriteFrame`).then(sf => {
            if(this.cityBg){
                this.cityBg.spriteFrame = sf
            }
            
        })
        ResManager.instance.loadAssetByUrl<SpriteFrame>(`cityImg/wall_${UserInfo.instance.race}/spriteFrame`).then(sf => {
            if(this.wallImage){
                this.wallImage.spriteFrame = sf
            }
            
        })


        let buildings: [BManager] = UserInfo.instance.villageData.bManager.bList
        let eventList: Events[] = UserInfo.instance.eventList

        let children = this.spaceList.children
        for (let i = 0; i < children.length; i++) {


            children[i].getComponent(Space).setData(buildings[i])
        }



    }
    /**点击地图上的建筑物 */
    onSpaceClick(data: { node: Node, eventTouch: EventTouch }) {


        data.node.getComponent(Space).onClick()
        //设置当前点击的坑位
        this.curClickSpace = data.node
    }
    /**点击地图上的背景 */
    onBgClick() {


        this.closeSpaceTip()
    }


    /**开启选择空地的提示 */
    openSpaceTip() {
        let children = this.spaceList.children
        for (let child of children) {
            child.getComponent(Space).openSpaceTip()
        }

        for (let ch of children) {
            let isCan = ch.getComponent(Space).canBuildOnSpace()
            if (isCan) {
                let nodePos = ch.worldPosition

                VillageCamera.instance.moveToWorldPos(nodePos)




                return
            }
        }
    }

    /**
     * 移动相机到指定的位置
     * @param uid 
     */
    moveCameraToSpace(uid: number) {
        let children = this.spaceList.children
        for (let ch of children) {
            let space = ch.getComponent(Space)
            if (space.buildData.uid == uid) {
                let nodePos = ch.worldPosition
                VillageCamera.instance.moveToWorldPos(nodePos)


            }
        }
    }

    /**关闭选择空地的提示 */
    closeSpaceTip() {
        let children = this.spaceList.children
        for (let child of children) {
            child.getComponent(Space).closeSpaceTip()
        }

        VillageController.instance.curClickSpace = null
        VillageController.instance.curBuilding = null
    }

}


